﻿
''' <summary>
''' Used to convert a regular expression to a value. One must inherit this class to implement custom logic.
''' </summary>
Public MustInherit Class RegularExpressionConverter(Of T)
    Protected Sub New()

    End Sub

    Public Overridable Function Convert(ByVal expression As RegularExpression) As T
        If expression Is Nothing Then Return Nothing

        Select Case expression.ExpressionType
            Case RegularExpressionType.Empty
                Return ConvertEmpty(TryCast(expression, Empty))
            Case RegularExpressionType.LiteralChar
                Return ConvertLiteralChar(TryCast(expression, LiteralChar))
            Case RegularExpressionType.Alternation
                Return ConvertAlternation(TryCast(expression, Alternation))
            Case RegularExpressionType.Concatenation
                Return ConvertConcatenation(TryCast(expression, Concatenation))
            Case RegularExpressionType.KleeneStar
                Return ConvertKleeneStar(TryCast(expression, KleeneStar))
            Case RegularExpressionType.LiteralRange
                Return ConvertLiteralRange(TryCast(expression, LiteralRange))
            Case Else
                Throw New ArgumentException("The expression type is not recognized.", "expression")
        End Select
    End Function

    Protected MustOverride Function ConvertEmpty(ByVal expression As Empty) As T

    Protected MustOverride Function ConvertLiteralChar(ByVal expression As LiteralChar) As T

    Protected MustOverride Function ConvertLiteralRange(ByVal expression As LiteralRange) As T

    Protected MustOverride Function ConvertAlternation(ByVal expression As Alternation) As T

    Protected MustOverride Function ConvertConcatenation(ByVal expression As Concatenation) As T

    Protected MustOverride Function ConvertKleeneStar(ByVal expression As KleeneStar) As T
        
End Class
